// NewsExtraInfoGetter.java

import org.jsoup.nodes.Document;

import java.sql.Timestamp;

/**
 * 新闻扩展信息爬取类
 */
public class NewsExtraInfoGetter {

    /**
     * 新闻扩展信息
     */
    static class NewsExtraInfo {
        private String source;  // 新闻来源
        private Timestamp release_time; // 新闻发布时间

        public NewsExtraInfo(String source, Timestamp release_time) {
            this.source = source;
            this.release_time = release_time;
        }

        public String getSource() {
            return source;
        }

        public void setSource(String source) {
            this.source = source;
        }

        public Timestamp getRelease_time() {
            return release_time;
        }

        public void setRelease_time(Timestamp release_time) {
            this.release_time = release_time;
        }
    }

    /**
     * 爬取网页，获取新闻扩展信息（{@link NewsExtraInfo}）
     *
     * @param news 新闻信息（{@link NewsInfoGetter.News}）
     * @return 新闻扩展信息（{@link NewsExtraInfo}），如果没有，则返回 {@code null}
     * @throws Exception
     */
    public static NewsExtraInfo getNewsExtraInfo(NewsInfoGetter.News news) throws Exception {
        HttpUtils httpUtils = HttpUtils.getInstance();
        httpUtils.setCharset("UTF-8");
        httpUtils.setTimeout(30000);
        try {
            // 获取新闻网页 html 源码
            Document document = httpUtils.executeGetAsDocument(news.getHref());
            // 解析 html 源码中的来源和发布时间信息
            String source = NewsExtraInfoParser.getSource(document);
            Timestamp releaseTime = NewsExtraInfoParser.getReleaseTime(news, document);
            // 如果二者至少有一个非空（可以解析出来）则返回信息
            if (source != null || releaseTime != null) {
                return new NewsExtraInfo(source, releaseTime);
            }
        } catch (Exception ignored) {
        }
        // 若二者均为空，则返回 null
        return null;
    }
}
